﻿using LogModule.Interface;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using NLog;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Animation;
using WorkshopModule.DbContexts;
using WorkshopModule.Models;
using WorkshopModule.Services;

namespace WorkshopModule.ViewModels {
    internal class WorkShopDetailUCViewModel : BindableBase, INavigationAware {
        //区域管理:依赖注入取出
        private readonly IRegionManager _regionManager;
        private readonly ILogService _logger;
        //数据库上下文
        private readonly WorkshopDbContext _dbContext;

        //接收信息
        int? workShopId;
        //命令
        public DelegateCommand GoBackCmm { get; set; }
        public DelegateCommand<string> DeviceStatus { get; set; }

        public DelegateCommand<string> DeviceStatus2 { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="dbContext"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public WorkShopDetailUCViewModel(ILogService logger, IRegionManager regionManager, WorkshopDbContext dbContext) {
            _logger = logger;
            _logger.Info("车间模块初始化");
            //参数空值校验
            _regionManager = regionManager ?? throw new ArgumentNullException(nameof(regionManager));
            _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));

            #region 初始化机台列表,一开始应该啥也没有
            DeviceDetailList = new ObservableCollection<DeviceDetail>();
            try {//初始化设置为空,后边再清吧
                ////var deviceList = dbContext.DeviceDetail.ToList();
                List<DeviceDetail> list = dbContext.DeviceDetail.ToList();
                foreach (var item in list) {
                    DeviceDetailList.Add(item);
                }
            } catch (Exception e) {
                MessageBox.Show(e.Message);
            }
          
            #endregion

            GoBackCmm = new DelegateCommand(GoBackCmmFunction);
            DeviceStatus = new DelegateCommand<string>(DeviceStatusConvert);
            DeviceStatus2 = new DelegateCommand<string>(DeviceStatusConvert2);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        private void DeviceStatusConvert(string obj) {
            try {
                DeviceDetailList.Clear();
                var list = _dbContext.DeviceDetail.Where((x) => x.DeviceStatus.ToString() == obj).Where((x) => workShopId == x.WorkShopId).ToList();
                foreach (var item in list) {
                    DeviceDetailList.Add(item);
                }
            } catch (Exception e) {
                MessageBox.Show(e.Message);
            }
        }
        private void DeviceStatusConvert2(string obj) {
            try {
                if (obj == "All") {
                    DeviceDetailList.Clear();
                    var list2 = _dbContext.DeviceDetail.ToList();
                    foreach (var item in list2) {
                        DeviceDetailList.Add(item);
                    }
                }
                if (obj != "All") {
                    DeviceDetailList.Clear();
                    var list = _dbContext.DeviceDetail.Where((x) => x.DeviceStatus.ToString() == obj).ToList();
                    foreach (var item in list) {
                        DeviceDetailList.Add(item);
                    }
                }
            } catch (Exception e) {
                MessageBox.Show(e.Message);
            }
        }

        /// <summary>
        /// 返回方法
        /// </summary>
        private void GoBackCmmFunction() {
            _regionManager.Regions["ContentRegion"].RequestNavigate("MonitorUC");
        }

        #region INavigationAware接口实现方法

        /// <summary>
        /// 接收参数
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnNavigatedTo(NavigationContext navigationContext) {
            if (navigationContext.Parameters.ContainsKey("WorkShopId")) {
                try {
                    workShopId = navigationContext.Parameters.GetValue<int>("WorkShopId");
                    DeviceDetailList.Clear();
                    var list = _dbContext.DeviceDetail.Where((x)=>workShopId == x.WorkShopId).ToList();
                    foreach (var item in list) {
                        DeviceDetailList.Add(item);
                    }
                } catch (Exception e) {
                    MessageBox.Show(e.Message);
                    _logger.Error("车间模块: WorkShopDetailUCViewModel:  ", e);
                }
              
            }
        }
        /// <summary>
        /// 是否重用实例
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool IsNavigationTarget(NavigationContext navigationContext) {
            return true;
        }
        /// <summary>
        /// 处理从当前视图导航离开时的逻辑​​。
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnNavigatedFrom(NavigationContext navigationContext) {
           
        }
        #endregion


        #region 集合属性

        private ObservableCollection<DeviceDetail> _DeviceDetailList;

        public ObservableCollection<DeviceDetail> DeviceDetailList {
            get { return _DeviceDetailList; }
            set {
                _DeviceDetailList = value;
                RaisePropertyChanged();
            }
        }
        #endregion
    }
}
